/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.grnt.buddy.BuddiesTreeControler.BuddiesCtlException;
import org.grnt.common.CommonFunc;
import org.grnt.dbcommon.ControlBean;
import static org.grnt.dbcommon.DataControler.TransOpt;

/**
 *
 * It is subclass of ControlBean to implement move a buddy for old parent to new
 * parent action.
 * @author sayid
 */
public class MoveBuddy implements ControlBean{
	final Long buddyId;
	final Long parentId;

	/**
	 *
	 * Constructor with two parameter on is buddy id another is parent id.
	 * @param buddyId
	 * @param parentId
	 */
	public MoveBuddy(Long buddyId, Long parentId) {
		this.buddyId = buddyId;
		this.parentId = parentId;
	}

	public List<String> getCommands() {
		List<String> result = new ArrayList<String> ();
		String command = "UPDATE gt_buddies_tree SET parent = ? WHERE buddy_id = ?";
		command = CommonFunc.sqlStrFormat(command, parentId.toString(), buddyId.toString());
		command = CommonFunc.sqlNullStr2NullVal(command);
		result.add(command);
		return result;
	}

	public boolean doInTransaction() {
		try {
			BuddiesTreeControler.movBuddy(buddyId, parentId, true);
			return true;
		} catch (BuddiesCtlException ex) {
			Logger.getLogger(MoveBuddy.class.getName()).log(Level.SEVERE, null, ex);
			return false;
		}
	}

	public void doAfterTransaction(TransOpt opt) { }
}
